import { defineConfig } from 'vite';
import commonjs from '@rollup/plugin-commonjs';
import path from 'path';
import { builtinModules } from 'module';
import pkg from './package.json';

const externalDeps = [
    ...builtinModules,
    ...builtinModules.map((m) => `node:${m}`), // node:fs、node:path 形式
    ...Object.keys(pkg.dependencies || {}),
];

export default defineConfig({
    build: {
        target: 'node22', // 或更高版本
        rollupOptions: {
            external: externalDeps,
            plugins: [
                commonjs({
                    dynamicRequireTargets: [
                        // ⛳️ 指定原生模块文件，让 dynamic require 生效
                        path.resolve(__dirname, 'node_modules/better-sqlite3/build/Release/better_sqlite3.node'),
                    ],
                    ignoreDynamicRequires: false,
                }),
            ],
        },
    },
});
